package com.baidu.mpks.schema.service;


import com.baidu.mpks.exception.BusinessException;
import com.baidu.mpks.permission.dao.PermissionDataRoleDao;
import com.baidu.mpks.permission.service.PermissionService;
import com.baidu.mpks.role.dao.RoleDao;
import com.baidu.mpks.schema.domain.UserSchemaAll;
import com.baidu.mpks.schema.dto.UserSchemaClassDto;
import com.baidu.mpks.utils.CommonUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class UserSchemaPermissionServiceImpl implements UserSchemaPermissionService {
    private static final Logger log = LoggerFactory.getLogger(UserSchemaPermissionServiceImpl.class);
    @Autowired
    PermissionService permissionService;
    @Autowired
    PermissionDataRoleDao permissionDataRoleDao;
    @Autowired
    RoleDao roleDao;
    @Value("${schema.host}")
    String schemaHost;
    @Value("${schema.getSchema}")
    String getSchemaApi;
    @Value("${schema.getClassList}")
    String getClassListApi;
    @Value("${schema.showClass}")
    String showClassApi;
    @Value("${schema.projectId}")
    String projectId;
    private static final ObjectMapper mapper = new ObjectMapper();

    public UserSchemaPermissionServiceImpl() {
    }

    public List<UserSchemaAll> getSchemaAll() throws IOException {
        new ArrayList();
        Map<String, String> getParam = new HashMap();
        getParam.put("projectId", this.projectId);
        getParam.put("version", "v2");
        String schemaUrl = this.schemaHost + this.getSchemaApi;
        JsonNode response = CommonUtils.httpGet(schemaUrl, getParam);
        if (response == null) {
            throw new BusinessException("请求失败");
        } else {
            int responseCode = response.get("errno").intValue();
            String responseMsg = "";
            if (response.get("msg") != null) {
                responseMsg = response.get("msg").asText();
            }

            JsonNode responseData = response.get("data");
            if (responseCode == 0 && responseData.get("@graph") != null) {
                String graph = mapper.writeValueAsString(responseData.get("@graph"));
                List<UserSchemaAll> schemaAll = (List)mapper.readValue(graph, new TypeReference<List<UserSchemaAll>>() {
                });
                return schemaAll;
            } else {
                throw new BusinessException(responseMsg);
            }
        }
    }

    public List<UserSchemaClassDto> getSchemaClassList(String runId, String type) throws IOException {
        new ArrayList();
        Map<String, String> getParam = new HashMap();
        getParam.put("projectId", this.projectId);
        if (runId != null) {
            getParam.put("runId", runId);
        }

        if (type != null) {
            getParam.put("type", type);
        }

        String schemaUrl = this.schemaHost + this.getClassListApi;
        JsonNode response = CommonUtils.httpGet(schemaUrl, getParam);
        if (response == null) {
            throw new BusinessException("请求失败");
        } else {
            int responseCode = response.get("errno").intValue();
            String responseMsg = "";
            if (response.get("msg") != null) {
                responseMsg = response.get("msg").asText();
            }

            JsonNode responseData = response.get("data");
            if (responseCode == 0 && responseData != null) {
                String data = mapper.writeValueAsString(responseData);
                List<UserSchemaClassDto> schemaClass = (List)mapper.readValue(data, new TypeReference<List<UserSchemaClassDto>>() {
                });
                return schemaClass;
            } else {
                throw new BusinessException(responseMsg);
            }
        }
    }

    public UserSchemaClassDto getSchemaClassById(Integer classId) throws IOException {
        new UserSchemaClassDto();
        Map<String, String> getParam = new HashMap();
        getParam.put("projectId", this.projectId);
        getParam.put("classId", classId.toString());
        String schemaUrl = this.schemaHost + this.showClassApi;
        JsonNode response = CommonUtils.httpGet(schemaUrl, getParam);
        if (response == null) {
            throw new BusinessException("请求失败");
        } else {
            int responseCode = response.get("errno").intValue();
            String responseMsg = "";
            if (response.get("msg") != null) {
                responseMsg = response.get("msg").asText();
            }

            JsonNode responseData = response.get("data");
            if (responseCode == 0 && responseData != null) {
                String data = mapper.writeValueAsString(responseData);
                UserSchemaClassDto schemaClass = (UserSchemaClassDto)mapper.readValue(data, new TypeReference<UserSchemaClassDto>() {
                });
                return schemaClass;
            } else {
                throw new BusinessException(responseMsg);
            }
        }
    }

    public Map<String, Set<String>> getAncestorMap(List<UserSchemaAll> schemaAll) {
        Map<String, Set<String>> result = new HashMap();
        List<UserSchemaAll> schemaClassList = (List)schemaAll.stream().filter((item) -> {
            return item.getAtType().equals("Class");
        }).collect(Collectors.toList());
        Map<String, Set<String>> classAtId2ParentListMap = (Map)schemaClassList.stream().collect(Collectors.toMap(UserSchemaAll::getAtId, UserSchemaAll::getSubClassOf));
        Iterator var5 = classAtId2ParentListMap.keySet().iterator();

        while(var5.hasNext()) {
            String key = (String)var5.next();
            Set<String> parentClassList = (Set)classAtId2ParentListMap.get(key);
            Set<String> recursiveResult = new HashSet();
            Set<String> value = this.recursiveList(parentClassList, classAtId2ParentListMap, recursiveResult);
            result.put(key, value);
        }

        return result;
    }

    public Set<String> getChildrenMapIncludeSelf(String classAtId, List<UserSchemaAll> schemaAll) {
        Map<String, Set<String>> childrenClassListMap = this.generatorChildrenMap(schemaAll);
        Set<String> result = new HashSet();
        result.add(classAtId);
        Set<String> childrenClassList = (Set)childrenClassListMap.get(classAtId);
        if (childrenClassList != null) {
            result.addAll((Collection)childrenClassListMap.get(classAtId));
        }

        return result;
    }

    private Map<String, Set<String>> generatorChildrenMap(List<UserSchemaAll> schemaAll) {
        Map<String, Set<String>> result = new HashMap();
        List<UserSchemaAll> schemaClassList = (List)schemaAll.stream().filter((item) -> {
            return item.getAtType().equals("Class");
        }).collect(Collectors.toList());
        Map<String, Set<String>> classAtId2ChildrenListMap = (Map)schemaClassList.stream().collect(Collectors.toMap(UserSchemaAll::getAtId, UserSchemaAll::getSupClassOf));
        Iterator var5 = classAtId2ChildrenListMap.keySet().iterator();

        while(var5.hasNext()) {
            String key = (String)var5.next();
            Set<String> childrenClassList = (Set)classAtId2ChildrenListMap.get(key);
            Set<String> recursiveResult = new HashSet();
            Set<String> value = this.recursiveList(childrenClassList, classAtId2ChildrenListMap, recursiveResult);
            result.put(key, value);
        }

        return result;
    }

    private Set<String> recursiveList(Set<String> iteratorList, Map<String, Set<String>> allMap, Set<String> result) {
        if (iteratorList != null && iteratorList.size() != 0) {
            Iterator var4 = iteratorList.iterator();

            while(var4.hasNext()) {
                String item = (String)var4.next();
                this.recursiveList((Set)allMap.get(item), allMap, result);
                result.add(item);
            }
        }

        return result;
    }
}